package com.example.demofdfs.test2;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.util.SerializationUtils;

import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;

public class TestA {

	
	@Test
	public void test_1() {
		int a = 18, b = 19;
		System.out.println(Integer.toBinaryString(a));
		System.out.println(Integer.toBinaryString(b));
		
		System.out.println(18 >> 1);
		System.out.println(19 >> 1);
		System.out.println(18 >> 2);
		System.out.println(19 >> 2);
		System.out.println(a ^ b);
		System.out.println(b ^ a);
	}
	
	@Test
	public void test_2() {
		Foo<String> foo = new Foo<>();
		System.out.println(foo);
		System.out.println(foo.getClass());
		System.out.println(foo.getClass().getComponentType());
		
		String[] s = new String[] {};
		String[] foos = foo.array(s);
		assert foos != s;
		System.out.println(foos);
		System.out.println(foos.length);
		System.out.println(foos.getClass());
		System.out.println(foos.getClass().getComponentType());
		
	}
	
	@Test
	public void test_3() {
		List<Foo<?>> link = new LinkedList<>();
		
		for (long i = 0; i < (Integer.MAX_VALUE + 10L); i++)
			link.add(new Foo<>(i));
		System.out.println(link.size());
		assert link.size() > Integer.MAX_VALUE;
	}
	
	@Test
	public void test_4() {
		Map<String, MyMap> map = new HashMap<>(0, 1F);
		map.put("a1", new MyMap());
		map.put("a2", new MyMap());
		map.put("a3", new MyMap());
		
		System.out.println(map);
	}
	
	@Test
    public void tableSizeFor() {
		int cap = 10;
        int n = cap - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        System.out.println(n);
//        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    }
	
	@Test
	public void test_5() {
		boolean flag = checkPassword("chen", "a6666");
		assert flag;
	}
	
	@Test
	public void test_6() {
		List<MyMap> list = new ArrayList<>();
		list.add(new MyMap());
		list.add(new MyMap());
		list.add(new MyMap());
		list.add(new MyMap());
		
		byte[] b = SerializationUtils.serialize(list);
		List<MyMap> l2 = (List<MyMap>) SerializationUtils.deserialize(b);
		System.out.println(list);
		System.out.println(l2);
		
		List<MyMap> l3 = (List<MyMap>) ((ArrayList<TestA.MyMap>)list).clone();
		System.out.println(l3);
		
		Spliterator<MyMap> split = list.spliterator();
		System.out.println(split);
		split.forEachRemaining(new Consumer<MyMap>() {

			@Override
			public void accept(MyMap t) {
				System.out.println(t);
			}
		});
	}
	
	public static boolean checkPassword(String userName, String password) {
		String regex = "^(?![A-Za-z\\d]+$)(?![A-Z\\W\\d]+$)(?![A-Za-z\\W]+$)"
				     + "(?![a-z\\d\\W]+$)\\S{8,20}$";
		String []keyRow = {"`1234567890-=","qwertyuiop[]\\","asdfghjkl;'","zxcvbnm,./"};
		String []keyCol = {"1qaz","2wsx","3edc","4rfv","5tgb","6yhn","7ujm","8ik,",
				"9ol.","0p;/","4esz","5rdx","6tfc","7ygv","8uhb","9ijn","0okm", 
				"-pl,","=[;."};
		if(!Pattern.matches(regex, password))
			return false;
		if(userName != null){
			if(password.toLowerCase().equals(userName.toLowerCase()))
				return false;
		}
		for(int i = 0; i < keyCol.length; i++)
			if(password.toLowerCase().contains(keyCol[i]) || 
					password.toLowerCase().contains(StringUtils.reverse(keyCol[i])))
				return false;
		for(int i = 0; i < keyRow.length; i++) {
			for(int j = 0; j <= keyRow[i].length() - 4; j++) {	
				String tmp = keyRow[i].substring(j, j + 4);
				if(password.toLowerCase().contains(tmp) ||
						password.toLowerCase().contains(StringUtils.reverse(tmp)))
					return false;
			}
		}
		return true;
	}
	
	@Test
	public void test_7() {
		LinkedList<String> list = new LinkedList<>();
		list.add("a");
		list.add("b");
		list.add("c");
		list.add("d");
		System.out.println(list);
	}
	
	@Test
	public void test_8() {
		int ii =  (1 << (32 - 16)) - 1;
		System.out.println(ii);
		int max = 32;
		int x = 1;
		for (int i = 0; i < max; i++)
			System.out.println(Integer.toBinaryString((x << i)) + "  ==  1 << " + i +" = " + ((x << i)));
	}
	
	@Test
	public void test_9() {
		int x = 10;
		for (int i = 1; i <= x; i++)
			System.out.println(i + " << 1 = " + (i << 1));
	}
	
	private static class Foo<F> {
		
		private long id;
		
		public Foo() {
			super();
		}

		public Foo(long id) {
			super();
			this.id = id;
		}

		@SuppressWarnings({ "unchecked" })
		public F[] array(F[] f) {
			f = (F[]) Array.newInstance(f.getClass().getComponentType(), 10);
			return  f;
		}
	}
	
	private static class MyMap implements Serializable {

		private static final long serialVersionUID = 1L;
		private int id;
		private String name;
		
		public MyMap() {
			this.id = new Random().nextInt();
		}

		@Override
		public String toString() {
			return "MyMap [id=" + id + "]";
		}
	}
}
